﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class UI_ctrl : MonoBehaviour
{
    public Dictionary<string, GameObject> view = new Dictionary<string, GameObject>();
    void load_all_object(GameObject root, string path)
    {
        foreach (Transform tf in root.transform)
        {
            if (this.view.ContainsKey(path + tf.gameObject.name))
            {
                Debugger.LogWarning("Warning object " + path + tf.gameObject.name + " is exist!");
                return;
            }
            this.view.Add(path + tf.gameObject.name, tf.gameObject);
            load_all_object(tf.gameObject, path + tf.gameObject.name + "/");
        }

    }

    public virtual void Awake()
    {
        this.load_all_object(this.gameObject, "");

    }

}


public class UIMgr : UnitySingleton<UIMgr> {

    private string root_prefab = "Prefabs/ui_prefabs/";
    private GameObject canvas;

    new void Awake()
    {
        base.Awake();
        canvas = GameObject.Find("Canvas");
        if (canvas == null)
            Debugger.LogError("UI manager load Canvas failed!!!!");

    }

    void Start () {
		
	}
	
	
	void Update () {
		
	}

    public UI_ctrl OpenWindow(string win)
    {
        GameObject obj = null;
        UI_ctrl ctrl = null;
        Type type = Type.GetType(win + "_UICtrl");
        foreach (Transform tf in canvas.transform)
        {
            if (tf.gameObject.name == win)
                obj = tf.gameObject;
        }
        if (obj != null)
        {
            obj.SetActive(true);
            ctrl = (UI_ctrl)obj.GetComponent(type);
        }
        else
        {
            GameObject ui_prefab = Resources.Load<GameObject>(root_prefab + win);
            GameObject ui_view = GameObject.Instantiate(ui_prefab);
            ui_view.transform.SetParent(canvas.transform, false);
            ui_view.name = win;
            ctrl = (UI_ctrl)ui_view.AddComponent(type);
        }
        return ctrl;
    }

    public void CloseWindow(string win)
    {
        Transform view = canvas.transform.Find(win);
        if (view)
        {
            view.gameObject.SetActive(false);
        }
    }

    public void CloseAllWindow()
    {
        List<Transform> child = new List<Transform>();
        foreach (Transform t in canvas.transform)
        {
            child.Add(t);
        }
        for (int i = 0; i < child.Count; i++)
        {
            child[i].gameObject.SetActive(false);
        }
    }

    public void add_button_listener(UI_ctrl ctrl, string view_name, UnityAction action)
    {
        Button bt = ctrl.view[view_name].GetComponent<Button>();
        if(bt == null)
        {
            Debugger.LogError("UIMgr Button BindUIEvent failed: no Button Component!");
            return;
        }
        bt.onClick.AddListener(action);
    }

    public void add_slider_listener(UI_ctrl ctrl,string view_name,UnityAction<float> action)
    {
        Slider slider = ctrl.view[view_name].GetComponent<Slider>();
        if (slider == null)
        {
            Debugger.LogError("UIMgr Slider BindUIEvent failed: no Slider Component!");
            return;
        }
        slider.onValueChanged.AddListener(action);
    }
}
